<?php

class FP_View_Helper_TimeSince
{
  public function timeSince($time){
    if ($time === null) {
      require_once 'Zend/View/Exception.php';
      throw new Zend_View_Exception('Expected parameter $time was not provided.');
    }

    require_once 'Zend/Date.php';
    //TODO NEEDS TEST
    if (!($time instanceof Zend_Date)) {
      $time = new Zend_Date($time);
    }

    $now = new Zend_Date();

//     logdebug('FP_View_Helper_TimeSince.timeSince', "Calculating diff between ". $time->toString() . ' and ' . $now->toString());

    $now->sub($time);

    $diff = $now->getTimestamp();

    if ($diff <= 50) {
      //less than 50 seconds
      return 'few seconds ago';
    } elseif ($diff > 50 && $diff < (60+30)) {
      //more than 50 seconds
      //less than minute and 30 seconds
      return 'a minute ago';
    } elseif ($diff >= (60+30) && $diff < (60*55)) {
      //more than minute and 30 seconds
      //less than 55 minutes
      $minutes = $diff / 60;
      $minutes = round($minutes, 0);
      return "$minutes minutes ago";
    } elseif ($diff >= (60*55) && $diff < (60*60+60*30)) {
      //more than 55 minutes
      //less than hour and 30 minutes
      return "an hour ago";
    } elseif ($diff >= (60*60+60*30) && $diff < (60 * 60 * 23.5)) {
      //more than hour and 30 minutes
      //less than 23 and half hour
      $hours = $diff / (60*60);
      $hours = round($hours, 0);
      return "$hours hours ago";
    } elseif ($diff >= (60 * 60 * 23.5) && $diff < (60 * 60 * 24 + 60 * 60 * 23)) {
      //more than 23 and half hour
      //less than two days and 23 hours
      return 'yesterday';
    }  else {
      $days = $diff / 86400;

      if ($days < 7)
	return intval($days) . " days ago";
      else if ($days >= 7 && $days < 14)
	return "last week";
      else if ($days >= 14 && $days < 30)
	return "two weeks ago";
      else if ($days >= 30 && $days < 60)
	return "last month";
      else {
	$months = $days / 30;
	if ($months < 12)
	  return intval($months) . " months ago";
	else if ($months >= 12 && $months < 24)
	  return "last year";
	else {
	  $years = $months / 12;
	  return intval($years) . " years ago";
	}
      }
    }



    /* elseif ($co) {
       return 'more then few seconds ago';
       }*/



  }
}

?>